# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=tox
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.0.0
pkgrel=1
pkgdesc="Python virtualenv management and testing tool (mingw-w64)"
arch=('any')
url="https://tox.readthedocs.io"
license=('GPL2')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools-scm"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools-scm"
             "${MINGW_PACKAGE_PREFIX}-python2-py"
             "${MINGW_PACKAGE_PREFIX}-python3-py"
             "${MINGW_PACKAGE_PREFIX}-python2-virtualenv"
             "${MINGW_PACKAGE_PREFIX}-python3-virtualenv"
             "${MINGW_PACKAGE_PREFIX}-python2-pluggy"
             "${MINGW_PACKAGE_PREFIX}-python3-pluggy"
             "util-linux")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://pypi.io/packages/source/t/tox/${_realname}-${pkgver}.tar.gz")
sha256sums=('96efa09710a3daeeb845561ebbe1497641d9cef2ee0aea30db6969058b2bda2f')

prepare() {
  cd "${srcdir}"
  find ${_realname}-${pkgver} -name "*.pyc" -delete
  
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

package_python3-tox() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-py"
           "${MINGW_PACKAGE_PREFIX}-python3-virtualenv"
           "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
           "${MINGW_PACKAGE_PREFIX}-python3-setuptools-scm"
           "${MINGW_PACKAGE_PREFIX}-python3-pluggy")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_python2-tox() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-py"
           "${MINGW_PACKAGE_PREFIX}-python2-virtualenv"
           "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
           "${MINGW_PACKAGE_PREFIX}-python2-setuptools-scm"
           "${MINGW_PACKAGE_PREFIX}-python2-pluggy")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i ${_f}
  done
  
  rename -v "tox" "tox2" "${pkgdir}${MINGW_PREFIX}"/bin/*
}

package_mingw-w64-i686-python2-tox() {
  package_python2-tox
}

package_mingw-w64-i686-python3-tox() {
  package_python3-tox
}

package_mingw-w64-x86_64-python2-tox() {
  package_python2-tox
}

package_mingw-w64-x86_64-python3-tox() {
  package_python3-tox
}
